package com.soecode.lyf.web;

import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.soecode.lyf.config.ImageUtils;
import com.soecode.lyf.config.SmsUtil;
import com.soecode.lyf.dto.Result;
import com.soecode.lyf.entity.Msg;
import com.soecode.lyf.entity.UserInfo;
import com.soecode.lyf.service.InfoService;
import com.soecode.lyf.service.UserService;

@Controller
//@Scope("session") // 表示会话级别
@RequestMapping("/keep") // url:/模块/资源/{id}/细分 /seckill/list
public class InfoController {
	
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private InfoService infoservice;
	@Autowired
	private UserService userservice;
	
	@ResponseBody
	@RequestMapping(value = "updateInfo", method = RequestMethod.GET)
	public Result UpdateUserInfo(String nickname, String signature,int age,String sex,String emial,String phone,String userid,Model model) {
		Result result = null;
		if(!StringUtils.isEmpty(nickname)&&!StringUtils.isEmpty(signature)&&!StringUtils.isEmpty(age)&&!StringUtils.isEmpty(sex)&&!StringUtils.isEmpty(emial)&&!StringUtils.isEmpty(phone)&&!StringUtils.isEmpty(userid)){
			System.out.println("请求进来了获取到了昵称"+nickname+'&'+signature+'&'+age+'&'+sex+'&'+emial+'&'+phone+'&'+userid);
			int i = infoservice.UpdateUserInfo(nickname, signature, age, sex, emial, phone, userid);
			if(i==0) {
			result = new Result(1,"修改个人信息失败");		
			}
			UserInfo info = userservice.getUserInfo(userid);
			System.out.println("个人信息"+info);
			model.addAttribute("USERINFO",info);
			result = new Result(0,model,"修改成功");	
		}else {
			result = new Result(1,"缺少参数");	
		}
		return result;
	};
	
	@ResponseBody
	@RequestMapping(value = "uploadimg", method = RequestMethod.POST)
	public Result UpdateUserImg(String userid, HttpServletRequest request, @RequestParam("file") MultipartFile[] pictureFile, Model model) {
		Result result = new Result();
		// 得到上传图片的地址
		System.out.println("查看"+pictureFile);
				String imgPath = null;
				//UserInfo userinfo = new UserInfo();
				try {
		            //ImageUtils为之前添加的工具类
					imgPath = ImageUtils.upload(request, pictureFile);
					if (imgPath != null) {
						// 将上传图片的地址封装到实体类
						//userinfo.setImg(imgPath);
						//将数据提交到数据库（包含文件和普通表单数据）
						int rowNo = infoservice.saveUserImg(imgPath,userid);
						if (rowNo > 0) {
							System.out.println("-----------------图片上传成功！");
							model.addAttribute("img",imgPath);
							result = new Result(0,model,"上传图片成功");
							// 转发：forword，重定向：redirect
							//return "redirect:/user/findUsers.do";
						} else {
							result = new Result(1,"上传图片失败");
			                System.out.println("-----------------图片上传失败！");
							//return "addUser";
						}
					}else{
		            }
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					System.out.println("----------------图片上传失败！");
				}
		return result;
	}
}
